package com.yjb.auth.config;

import com.yjb.auth.component.UserDetailsServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.token.AccessTokenConverter;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import javax.sql.DataSource;
import java.util.Arrays;

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private DataSource dataSource;
    @Autowired
    AuthenticationManager authenticationManager;
    @Autowired
    AccessTokenConverter accessTokenConverter;
    @Autowired
    UserDetailsServiceImpl userDetailsService;
    @Autowired
    public JwtTokenStore tokenStore;

    //第三方客户端接入信息的储存地址;
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //第三方客户端信息存入数据库
        clients.jdbc(dataSource);
/*       clients.inMemory().withClient("client")
                .secret(passwordEncoder.encode("secret"))
                .authorizedGrantTypes("authorization_code", "refresh_token", "implicit", "client_credentials")
                .redirectUris(gateway)
                .accessTokenValiditySeconds(3000)
                .autoApprove(true)
                .scopes("all", "read");*/

    }

    //token设置
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
/*        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        tokenEnhancerChain.setTokenEnhancers(Arrays.asList(accessTokenConverter));
        endpoints.tokenEnhancer(tokenEnhancerChain);*/

        endpoints
                //jwt配置
                .accessTokenConverter(accessTokenConverter)
                .tokenStore(tokenStore)
                //密码模式必须配置此项
                .authenticationManager(authenticationManager)
                //密码模式refresh_token必须配置此项
                .userDetailsService(userDetailsService);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) {
        security
                .tokenKeyAccess("isAuthenticated()")    //获取token
                .checkTokenAccess("isAuthenticated()")  //验证token
                //密码模式必须配置此项
                .allowFormAuthenticationForClients();

    }


}
